package com.findMoneyGame.pages.game.controllers {
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;

	import com.findMoneyGame.pages.game.GameLevelPage;
	import com.findMoneyGame.pages.game.controllers.Controller;
	import com.findMoneyGame.tools.TextFormatProvider;

	public class HintController extends Controller {
		private function get hintButton () { return _gameLevelPage.hintButton; }

		public function HintController (gameLevelPage : GameLevelPage) {
			super(gameLevelPage);
			hintButton.price = gameLevel.data.hintPrice;
			hintButton.addEventListener(MouseEvent.CLICK, hintButtonClick);
		}

		private function hintButtonClick (e : Event) {
			if (gameImage.hintEnabled) {
				return;
			} else if (user.moneyCount >= gameLevel.data.hintPrice) {
				user.takeMoney(gameLevel.data.hintPrice);
				gameImage.enableHint();
				var text = initTextField(gameLevel.data.hintTime + 'c');
				hintButton.addChild(text);
				var hintTimer = new Timer(1000, gameLevel.data.hintTime);
				hintTimer.addEventListener(
					TimerEvent.TIMER, 
					function (e : Event) { text.text = (gameLevel.data.hintTime - hintTimer.currentCount) + 'с'; });
				hintTimer.addEventListener(
					TimerEvent.TIMER_COMPLETE, 
					function (e : Event) {
						hintButton.removeChild(text);
						gameImage.disableHint();
					});
				hintTimer.start();
			} else {
				_gameLevelPage.dispatchEvent(new Event(GameLevelPage.NEED_MORE_MONEY));
			}
		}
		private function initTextField (s : String) : TextField {
			var format = TextFormatProvider.getFormat(30, 0xFFFFFF);
			var text = new TextField();
			text.x = 10;
			text.y = 6;
			text.width = 70;
			text.height = 70;
			text.autoSize = TextFieldAutoSize.LEFT;
			text.selectable = false;
			text.setTextFormat(format);
			text.defaultTextFormat = format;
			text.text = s;
			return text;
		}
	}
}